package cn.ecut.controller;

import cn.ecut.model.*;
import cn.ecut.service.ScholarshipApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/scholarshipApply")
public class ScholarshipApplyController {
    @Autowired
    private ScholarshipApplyService scholarshipApplyService;

    @ResponseBody
    @RequestMapping("/test")
    public void test1(String relationShip, String relationName, String relationOccupation , String relationPhone, ScholarshipApply scholarshipApply,String certificate){
        System.out.println("--------------------------------");
        System.out.println(scholarshipApplyService.TransRelative(relationShip, relationName, relationOccupation, relationPhone));
        System.out.println(scholarshipApply);
        System.out.println(scholarshipApplyService.jsonTrans(certificate));

    }

    @ResponseBody
    @RequestMapping("/insert")
    public Message insert(HttpSession httpSession,String relationShip, String relationName, String relationOccupation , String relationPhone, ScholarshipApply scholarshipApply, String certificate){
        Message message = new Message();
        Date date = new Date();// 获取当前时间
        List<Relative> relativeList = new ArrayList<>();
        relativeList=scholarshipApplyService.TransRelative(relationShip, relationName, relationOccupation, relationPhone);
        for (int i=0;i<relativeList.size();i++){
            relativeList.get(i).setNumber((String) httpSession.getAttribute("number"));
            relativeList.get(i).setSid(scholarshipApply.getSid());
        }
        int role1=scholarshipApplyService.insertRelative(relativeList);
        if(role1!=0){
            scholarshipApply.setCertificate(scholarshipApplyService.jsonTrans(certificate));
            scholarshipApply.setNumber((String) httpSession.getAttribute("number"));
            scholarshipApply.setApplyTime(date);
            scholarshipApply.setProvince(scholarshipApplyService.selectArea(scholarshipApply.getProvince()));
            scholarshipApply.setCity(scholarshipApplyService.selectArea(scholarshipApply.getCity()));
            scholarshipApply.setCounty(scholarshipApplyService.selectArea(scholarshipApply.getCounty()));
            int role2=scholarshipApplyService.insertScholarshipApply(scholarshipApply);
            if(role2==1){
                scholarshipApplyService.updataApplyNum(scholarshipApply.getSid());
                message.setFlag(true);
            }else {
                message.setFlag(false);
                message.setMsg("申请失败");
            }
        }else {
            message.setFlag(false);
            message.setMsg("亲属关系插入失败");
        }
        return message;
    }
    @ResponseBody
    @RequestMapping("/check")
    public Message check(Integer sid,HttpSession session){
        Message message = new Message();
        String number =(String)session.getAttribute("number");
        if(scholarshipApplyService.selectScholarshipResidue(sid)==0){
            message.setFlag(false);
            message.setMsg("已没有名额");
        }else if(scholarshipApplyService.selectIsApply(number,sid)==false){
            message.setFlag(false);
            message.setMsg("你已经申请了该奖学金");
        }else if(scholarshipApplyService.IsOutCount(number)==false){
            message.setFlag(false);
            message.setMsg("可同时申请数已达到最大");
        }else if(scholarshipApplyService.selectIsHave(number)==false){
            message.setFlag(false);
            message.setMsg("已获得奖学金,不可在申请");
        }else{
            message.setFlag(true);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/showExamine")
    public HashMap<String, Object> showExamine(int page, int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Examine> list = new ArrayList<>();
        int offset = (page - 1) * limit;
        list = scholarshipApplyService.selectExamineByPage(offset, limit);
        Integer count = scholarshipApplyService.selectAllApplyCount();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/selectExamine")
    public HashMap<String, Object> selectExamine(String searchResult ,int page ,int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Examine> list = new ArrayList<>();
        int offset = (page - 1) * limit;
        list = scholarshipApplyService.advanceSelect(searchResult, offset, limit);
        System.out.println(page);
        System.out.println(limit);
        Integer count = scholarshipApplyService.advanceSelectCount(searchResult);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("/deleteScholarshipApply")
    public Message deleteScholarshipApply(Integer id){
        Message message = new Message();
        int state = scholarshipApplyService.selectScholarshipApplyStateByid(id);
        if(state==0){
            message.setFlag(false);
            message.setMsg("还未对该申请单进行过操作,无法删除");
        }else {
            scholarshipApplyService.deleteScholarshipApplyByid(id);
            message.setFlag(true);
        }
        return message;
    }
    @ResponseBody
    @RequestMapping("/edit")
    public Message edit(Integer id,String number){
        Message message = new Message();
        HashMap<String,Object> hashMap = new HashMap<>();
        Student student = scholarshipApplyService.selectStudentAndRelative(number,scholarshipApplyService.selectSidByid(id));
        List<Relative> relativeList = student.getRelativeList();
        ScholarshipApply scholarshipApply = scholarshipApplyService.selectScholarship(id);
        hashMap.put("student",student);
        hashMap.put("relative",relativeList);
        hashMap.put("scholarshipApply",scholarshipApply);
        message.setData(hashMap);
        return message;
    }

    @ResponseBody
    @RequestMapping("/pass")
    public Message pass(Integer id){
        Message message = new Message();
        int role=scholarshipApplyService.pass(id);
        if(role==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("操作失败");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/out")
    public Message out(Integer id){
        Message message = new Message();
        int role=scholarshipApplyService.out(id);
        if(role==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("操作失败");
        }
        return message;
    }
}
